//
//  DetailPicView.m
//  iNews
//
//  Created by lanou3g on 14-12-10.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "DetailPicView.h"
#import "DetailModel.h"
#import "DetailArchiveModel.h"
#import "UIImage+ImageDown.h"

#import "UIImageView+WebCache.h"

#define kPicturePath @"/picture"

@implementation DetailPicView

-(void)dealloc
{
    [_scroll release];
    [_label release];
    [_numLab release];
    [_textV release];
    [_rightButton release];
    [_leftButton release];
    [_postid release];
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor blackColor];
        
        [self addAllViews];
        
    }
    return self;
}


- (void)addAllViews
{
    self.scroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 250)] autorelease];
    self.scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 250);
    self.scroll.pagingEnabled = YES;
 //   self.scroll.backgroundColor = [UIColor blueColor];
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator = NO;
    [self addSubview:self.scroll];
    
    //标题
    self.label = [[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.scroll.frame)+5, [UIScreen mainScreen].bounds.size.height * 2/3.0, [UIScreen mainScreen].bounds.size.width - 50, 30)] autorelease];
    self.label.textColor = [UIColor whiteColor];
    self.label.font = [UIFont systemFontOfSize:15];
  //  self.label.backgroundColor = [UIColor grayColor];
    [self addSubview:self.label];
    
    //显示页数
    self.numLab = [[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.label.frame) , CGRectGetMinY(self.label.frame), 50, CGRectGetHeight(self.label.frame))] autorelease];

    self.numLab.textColor = [UIColor whiteColor];
   // self.numLab.backgroundColor = [UIColor blueColor];
    [self addSubview:self.numLab];
    
    //详情
    self.textV = [[[UITextView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.label.frame), CGRectGetMaxY(self.label.frame), [UIScreen mainScreen].bounds.size.width - 10, 100)] autorelease];
    self.textV.textColor = [UIColor whiteColor];
    self.textV.backgroundColor = [UIColor clearColor];
//    self.textV.alpha = 0;
    self.textV.editable = NO;
    [self addSubview:self.textV];
    
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.leftButton.frame = CGRectMake(10, 20, 30, 20);
    [self.leftButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [self addSubview:self.leftButton];
    
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.rightButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 10 - 30, 20, 30, 30);
   // [self.rightButton setTitle:@"收藏" forState:UIControlStateNormal];
    [self addSubview:self.rightButton];
    
    
}

- (void)setMessageWithModel:(DetailModel *)model
{
    CGRect rect = self.scroll.frame;
   // CGPoint cen = self.scroll.center;
    
    self.scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * model.photos.count, rect.size.height);
    
    self.array = [NSMutableArray array];
  //  self.noteArr = [NSMutableArray array];
    for (int i = 0; i < model.photos.count; i++) {
        
        UIImageView *imV = [[[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width,  CGRectGetHeight(self.scroll.frame))] autorelease];
        
        [self.scroll addSubview:imV];
        
        //使用第三方方法加载图片
        [imV sd_setImageWithURL:model.photos[i][@"imgurl"] placeholderImage:[UIImage imageNamed:@"placehodel.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
           
            if (image != nil) {
                
                float t = (float)image.size.height / image.size.width;
                //设置scroll的frame和imv的frame
                if (t * [UIScreen mainScreen].bounds.size.width >= rect.size.height) {
                    
                    self.scroll.frame = CGRectMake(0, rect.origin.y, [UIScreen mainScreen].bounds.size.width, t * [UIScreen mainScreen].bounds.size.width);
                    self.scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * model.photos.count , t * [UIScreen mainScreen].bounds.size.width);
                    
                    imV.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width, t * [UIScreen mainScreen].bounds.size.width);
                    
                }
                
                imV.image = image;
                //图片收藏
                //自定义队列(1.创建队列 2.创建任务)
                
                [self.array addObject:image];
                
            }
            
        }];
        
        /*
        imV.image = [UIImage imageWithUrlString:model.photos[i][@"imgurl"] myBlock:^(UIImage *image) {
     
            if (image != nil) {
                
                float t = (float)image.size.height / image.size.width;
                //设置scroll的frame和imv的frame
                if (t * [UIScreen mainScreen].bounds.size.width >= rect.size.height) {
                    
                    self.scroll.frame = CGRectMake(0, rect.origin.y, [UIScreen mainScreen].bounds.size.width, t * [UIScreen mainScreen].bounds.size.width);
                    self.scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * model.photos.count , t * [UIScreen mainScreen].bounds.size.width);
                    
                    imV.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width, t * [UIScreen mainScreen].bounds.size.width);
                   
                }
                
                imV.image = image;
                //图片收藏
                //自定义队列(1.创建队列 2.创建任务)
                
                [self.array addObject:image];

            }
            
        }];
         */
//        if (model.photos[i][@"note"] != nil) {
//            
//            [self.noteArr addObject:model.photos[i][@"note"]];
//        }
        
     //   [self.noteArr addObject:model.photos[i][@"note"]];
        
        self.textV.text = model.photos[i][@"note"];
        
    }
    
    self.numLab.text = [NSString stringWithFormat:@"1/%lu",(unsigned long)model.photos.count];
    
    self.label.text = model.setname;
}



- (void)writeMessageDAMWithModel:(DetailArchiveModel *)m
{
    CGSize size = self.scroll.contentSize;
    // CGPoint cen = self.scroll.center;
    CGRect rect = self.scroll.frame;
    
    NSLog(@"%@",m.setname);
    if (m.imageArray.count != 0) {
        
    self.scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * m.imageArray.count, size.height);
        
        
    for (int i = 0; i < m.imageArray.count; i++) {
        
        UIImage *image = m.imageArray[i];
        
        UIImageView *imV = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width,  CGRectGetHeight(self.scroll.frame))] ;
        
        float t = (float)image.size.height / image.size.width;
        //附图片
        if (t * [UIScreen mainScreen].bounds.size.width >= rect.size.height) {
            
            self.scroll.frame = CGRectMake(0, rect.origin.y, [UIScreen mainScreen].bounds.size.width, t * [UIScreen mainScreen].bounds.size.width);
            self.scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * m.imageArray.count , t * [UIScreen mainScreen].bounds.size.width);
            
            imV.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width, t * [UIScreen mainScreen].bounds.size.width);
            
        }
        
        imV.image = m.imageArray[i];
        [self.scroll addSubview:imV];
        
        [imV release];
        
        //   p.dPicView.textV.text = m.noteArray[i];
    }
    
    self.numLab.text = [NSString stringWithFormat:@"1/%lu",(unsigned long)m.imageArray.count];

    self.label.text = m.setname;
    }
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
